package com.xatime.auth.api;

import lombok.AllArgsConstructor;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;
import org.springframework.util.Assert;

/**
 * Created by mason on 18-6-12.
 */
@Configuration
@EnableResourceServer
@EnableConfigurationProperties(Oauth2Properties.class)
@AllArgsConstructor
public class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {

    private JwtTokenStore jwtTokenStore;
    private Oauth2Properties oauth2Properties;

    @Override
    public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
        Assert.hasText(oauth2Properties.getResourceId(), "resource id cannot be blank");

        resources.tokenStore(jwtTokenStore).resourceId(oauth2Properties.getResourceId());
    }

    @Bean
    public UserDetailsService userDetailsService() {
        return new NoneUserDetailsService();
    }
}
